package com.chb.tenant;

import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import com.chb.properties.TenantProperties;
import com.chb.properties.TestProperties;
import lombok.AllArgsConstructor;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.StringValue;

import javax.annotation.Resource;

/**
 * @description:
 * @author: CHB
 * @create: 2022-04-12 14:27
 **/
@AllArgsConstructor
public class TenantHandler implements TenantLineHandler {

    @Resource
    private TenantProperties properties;

    @Resource
    private TestProperties testProperties;

    public TenantHandler(TenantProperties properties){
        this.properties = properties;
    }

    @Override
    public Expression getTenantId() {
        return new StringValue(testProperties.getTenantName());
    }

    @Override
    public String getTenantIdColumn() {
        return properties.getColumn();
    }

    /**
     * 过滤租户表
     *
     * @param tableName 表名
     * @return 是否忽略, true:表示忽略，false:需要解析并拼接多租户条件
     */
    @Override
    public boolean ignoreTable(String tableName) {
        return false;
        // 校验忽略的表
        // if (this.properties.getIgnoreTables() != null && this.properties.getIgnoreTables().size() > 0) {
        //     for (String ignoreTable : this.properties.getIgnoreTables()) {
        //         if (tableName.matches(ignoreTable)) {
        //             return true;
        //         }
        //     }
        // }

        // 校验待拼接租户表
        // if (this.properties.getTables() != null && this.properties.getTables().size() > 0) {
        //     for (String table : this.properties.getTables()) {
        //         if (tableName.matches(table)) {
        //             return false;
        //         }
        //     }
        // }
        //
        // return true;
    }
}
